library(lattice)
library(Hmisc)

sep.d <- read.table('makespan_all.txt', header=TRUE)

sep.d$RUNTIME <- round(sep.d$RUNTIME/3600)

sep.df <- reshape(sep.d, v.names="MAKESPAN", idvar=c("TASK","ESCALONADOR","CHECK","RUNTIME"), timevar="GREEN", direction="wide")

#sep.df$MAKESPAN.SLEEP <- sep.df$MAKESPAN.SLEEP / sep.df$MAKESPAN.OFF
#sep.df$MAKESPAN.STANDBY <- sep.df$MAKESPAN.STANDBY / sep.df$MAKESPAN.OFF

stats <- aggregate(sep.df$MAKESPAN.SLEEP, list(sep.df$RUNTIME,sep.df$ESCALONADOR,sep.df$CHECK), mean)

colnames(stats) <- c("RUNTIME","ESCALONADOR","CHECK","MEAN_MAKESPAN.SLEEP")

stats$MEAN_MAKESPAN.STANDBY <- aggregate(sep.df$MAKESPAN.STANDBY, list(sep.df$RUNTIME,sep.df$ESCALONADOR,sep.df$CHECK), mean)$x

stats$MEAN_MAKESPAN.OFF <- aggregate(sep.df$MAKESPAN.OFF, list(sep.df$RUNTIME,sep.df$ESCALONADOR,sep.df$CHECK), mean)$x

stats$MEAN_RUNTIME <- aggregate(sep.df$RUNTIME, list(sep.df$RUNTIME,sep.df$ESCALONADOR,sep.df$CHECK), mean)$x

stats$N <- aggregate(sep.df$TASK, list(sep.df$RUNTIME,sep.df$ESCALONADOR,sep.df$CHECK), length)$x

stats$SD_MAKESPAN.SLEEP <- aggregate(sep.df$MAKESPAN.SLEEP, list(sep.df$RUNTIME,sep.df$ESCALONADOR,sep.df$CHECK), sd)$x

stats$SD_MAKESPAN.STANDBY <- aggregate(sep.df$MAKESPAN.STANDBY, list(sep.df$RUNTIME,sep.df$ESCALONADOR,sep.df$CHECK), sd)$x

stats$SD_MAKESPAN.OFF <- aggregate(sep.df$MAKESPAN.OFF, list(sep.df$RUNTIME,sep.df$ESCALONADOR,sep.df$CHECK), sd)$x

stats$UPPER_MAKESPAN.SLEEP <- stats$MEAN_MAKESPAN.SLEEP + qnorm(0.975)*stats$SD_MAKESPAN.SLEEP/sqrt(stats$N)

stats$LOWER_MAKESPAN.SLEEP <- stats$MEAN_MAKESPAN.SLEEP - qnorm(0.975)*stats$SD_MAKESPAN.SLEEP/sqrt(stats$N)

stats$UPPER_MAKESPAN.STANDBY <- stats$MEAN_MAKESPAN.STANDBY + qnorm(0.975)*stats$SD_MAKESPAN.STANDBY/sqrt(stats$N)

stats$LOWER_MAKESPAN.STANDBY <- stats$MEAN_MAKESPAN.STANDBY - qnorm(0.975)*stats$SD_MAKESPAN.STANDBY/sqrt(stats$N)

stats$UPPER_MAKESPAN.OFF <- stats$MEAN_MAKESPAN.OFF + qnorm(0.975)*stats$SD_MAKESPAN.OFF/sqrt(stats$N)

stats$LOWER_MAKESPAN.OFF <- stats$MEAN_MAKESPAN.OFF - qnorm(0.975)*stats$SD_MAKESPAN.OFF/sqrt(stats$N)

stats2 <- reshape(stats, idvar=c("RUNTIME","ESCALONADOR","CHECK"), v.names=c("MEAN_MAKESPAN","SD_MAKESPAN","UPPER_MAKESPAN","LOWER_MAKESPAN"), direction="long", varying=list(c("MEAN_MAKESPAN.OFF","MEAN_MAKESPAN.SLEEP","MEAN_MAKESPAN.STANDBY"), c("SD_MAKESPAN.OFF","SD_MAKESPAN.SLEEP","SD_MAKESPAN.STANDBY"),c("UPPER_MAKESPAN.OFF","UPPER_MAKESPAN.SLEEP","UPPER_MAKESPAN.STANDBY"),c("LOWER_MAKESPAN.OFF","LOWER_MAKESPAN.SLEEP","LOWER_MAKESPAN.STANDBY")),times=c("OFF","SLEEP","STANDBY"), timevar="GREEN")

print(xYplot(Cbind(MEAN_MAKESPAN,LOWER_MAKESPAN,UPPER_MAKESPAN) ~ MEAN_RUNTIME | ESCALONADOR*CHECK, data=stats2, groups=GREEN, auto.key=TRUE, type="p"))
